package br.ufrj.dcc.mapaviolencia.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import br.ufrj.dcc.mapaviolencia.util.DBUtil;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

@Path("/noticias")
public class NoticiaREST {

	Logger logger = Logger.getLogger(NoticiaREST.class);
	
	@GET
	@Produces(MediaType.APPLICATION_JSON + "; charset=UTF-8")
	public String getAllNoticias() {
		DBCollection collection = DBUtil.getInstance().getDatabase().getCollection(DBUtil.COLLECTION_NOTICIAS);
		DBCursor cursor = collection.find();
		StringBuilder json = new StringBuilder();
		json.append("[");
		for (DBObject o : cursor) {
			json.append(o.toString()+",\n");
		}
		json.append("]");
		 GsonBuilder gsonBuilder = new GsonBuilder();
		 Gson gson = gsonBuilder.setPrettyPrinting().create();
		 
		
		return gson.toJson(new JsonParser().parse(json.toString()));
//		return json.toString();
	}
	
}
